package com.ruoyi.orderEat.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.orderEat.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.orderEat.service.IOrderInfoService;

import static com.ruoyi.common.core.page.TableSupport.getPageDomain;

/**
 * 订单信息Controller
 * 
 * @author dinghao
 * @date 2023-12-06
 */
@RestController
@RequestMapping("/orderEat/orderInfo")
public class OrderInfoController extends BaseController
{
    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 小程序查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:orderinfo:list', 'wx'})")
    @GetMapping("/wxList")
    public AjaxResult wxList(OrderInfo orderInfo)
    {
        PageDomain pageDomain = getPageDomain();
        List<OrderInfoVO> orderInfoVOS = orderInfoService.selectOrderInfoList(orderInfo,pageDomain.getPageNum(),pageDomain.getPageSize());
        return success(orderInfoVOS);
    }

    /**
     * 小程序查询用户的餐桌号
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:orderinfo:list', 'wx'})")
    @GetMapping("/getWxTable")
    public AjaxResult getWxTable()
    {
        Integer tableId = orderInfoService.queryWxTable();
        return success(tableId);
    }

    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo)
    {
        startPage();
        PageInfo<OrderInfo> orderInfoPageInfo = orderInfoService.selectOrderInfos(orderInfo);
        return  getDataTable(orderInfoPageInfo.getList());
    }

    /**
     * 管理后台查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:list')")
    @GetMapping("/zList")
    public AjaxResult zList( )
    {
        List<OrderListVO> orderListVOS = orderInfoService.selectOrderList(new OrderInfo());
        return success(orderListVOS);
    }

    /**
     * 管理后台查询订单详情列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:list')")
    @GetMapping("/getOrderDetail/{orderId}")
    public AjaxResult getOrderDetail(@PathVariable Long orderId)
    {
        List<ShopCart> orderDetailList = orderInfoService.selectOrderDetailList(orderId);
        return success(orderDetailList);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo)
    {
        PageInfo<OrderInfo> orderInfoPageInfo = orderInfoService.selectOrderInfos(orderInfo);
        List<OrderInfo> list = orderInfoPageInfo.getList();
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(orderInfoService.selectOrderInfoByOrderId(orderId));
    }

    /**
     * 新增用户与餐桌绑定关系
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:orderinfo:add','wx'})")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping("/addTableAndUser")
    public AjaxResult addTableAndUser(@RequestBody OrderInfo orderInfo)
    {
        orderInfoService.saveTableAndUser(orderInfo.getTableId());
        return success();
    }

    /**
     * 小程序新增订单信息
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:orderinfo:add','wx'})")
    @Log(title = "小程序新增订单信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody OrderShop orderShop)
    {
        return toAjax(orderInfoService.insertOrderInfo(orderShop));
        //return toAjax(orderInfoService.ceshi(orderShop));
    }

    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody OrderShop orderShop)
    {
        return toAjax(orderInfoService.insertOrderList(orderShop));
    }

    /**
     * 结算订单
     */
    //@PreAuthorize("@ss.hasPermi('orderEat:orderinfo:edit')")
    @PreAuthorize("@ss.hasPermi({'orderEat:orderinfo:edit','wx'})")
    @Log(title = "结算订单", businessType = BusinessType.UPDATE)
    @PostMapping("/toSettlement")
    public AjaxResult toSettlement(@RequestBody OrderInfo orderInfo)
    {
        orderInfoService.updateOrderInfo(orderInfo);
        return success();
    }

    /**
     * 修改主订单详情信息
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:edit')")
    @Log(title = "修改主订单详情", businessType = BusinessType.UPDATE)
    @PostMapping("/editOrderDetail")
    public AjaxResult editOrderDetail(@RequestBody OrderInfoVO orderInfoVO)
    {
        orderInfoService.updateOrderDetail(orderInfoVO.getShopCartList(),orderInfoVO.getOrderId());
        return success();
    }

    /**
     * 修改追加订单详情信息
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:edit')")
    @Log(title = "修改追加订单详情", businessType = BusinessType.UPDATE)
    @PostMapping("/editOrderAssociate")
    public AjaxResult editOrderAssociate(@RequestBody OrderInfoVO orderInfoVO)
    {
        orderInfoService.updateOrderAssociateDetail(orderInfoVO);
        return success();
    }


    /**
     * 小程序确认下单
     */
    @PreAuthorize("@ss.hasPermi({'orderEat:orderinfo:edit','wx'})")
    @Log(title = "小程序确认下单", businessType = BusinessType.UPDATE)
    @PostMapping("/toPay")
    public AjaxResult toPay(@RequestBody OrderInfo orderInfo)
    {
        orderInfoService.updateWxOrder(orderInfo);
        return success();
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(orderInfoService.deleteOrderInfoByOrderIds(orderIds));
    }


    /**
     * 删除主订单
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:remove')")
    @Log(title = "删除主订单", businessType = BusinessType.DELETE)
    @PostMapping("/deleteOrderInfo/{orderId}")
    public AjaxResult deleteOrderInfo(@PathVariable Long orderId)
    {
        orderInfoService.deleteOrderInfo(orderId);
        return success();
    }

    /**
     * 删除追加订单
     */
    @PreAuthorize("@ss.hasPermi('orderEat:orderinfo:remove')")
    @Log(title = "删除追加订单", businessType = BusinessType.DELETE)
    @PostMapping("/deleteOrderAssociate/{orderId}")
    public AjaxResult deleteOrderAssociate(@PathVariable Long orderId)
    {
        orderInfoService.deleteOrderAssociateByOrderAssociatedId(orderId);
        return success();
    }

}
